//
//  WorkerInfoTableViewCell.m
//  work
//
//  Created by 徐宏飞 on 16/6/3.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "WorkerInfoTableViewCell.h"
#import "WorkerInfoModel.h"
#import "NSDate+CL.h"

@interface WorkerInfoTableViewCell ()

@property (nonatomic, strong) UIImageView *imgIcon;      //头像
@property (nonatomic, strong) UILabel *lblClassification;//信誉等级
@property (nonatomic, strong) UILabel *lblName;          //姓名
@property (nonatomic, strong) UILabel *lblProfession;    //工种
@property (nonatomic, strong) UILabel *lblPrice;         //价格
@property (nonatomic, strong) UILabel *lblIDnumber;      //身份证号
@property (nonatomic, assign) NSInteger price;

@property (nonatomic, strong) UIButton *btnSignIn;       //签到

@property (nonatomic, strong) UILabel *lblCheckInCount;  //签到次数

@end


@implementation WorkerInfoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        self.backgroundColor = WHITE;
        
        //价格
        _price = 1;
        
        //icon
        [self addSubview:self.imgIcon];
        [self addSubview:self.lblClassification];
        [self addSubview:self.lblName];
        [self addSubview:self.lblProfession];
        [self addSubview:self.lblPrice];
        [self addSubview:self.lblIDnumber];
        
        [self addSubview:self.btnSignIn];
        [self addSubview:self.lblCheckInCount];
        
    }
    return self;
}





#pragma mark - event response
- (void)btnSignInAction{
    NSLog(@"签到按钮");
    
    [self.delegate workerInfoBtnWorkerCheckinAction];
    
}



#pragma mark - private methods
- (UILabel *)createLableFrame:(CGRect)frame textColor:(UIColor *)color font:(UIFont *)font text:(NSString *)text{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textColor = color;
    label.font = font;
    label.text = text;
    label.numberOfLines = 0;
    //    label.lineBreakMode =
    return label;
}


#pragma mark - gettsers and setters
- (UIImageView *)imgIcon{
    if (!_imgIcon) {
        _imgIcon = [[UIImageView alloc] initWithFrame:CGRectMake(15*WID, 25*WID, 80*WID, 80*WID)];
        _imgIcon.image = [UIImage imageNamed:@"worker_icon"];
    }
    return _imgIcon;
}


- (UILabel *)lblClassification{
    if (!_lblClassification) {
        _lblClassification = [self createLableFrame:CGRectMake(0, 0, 150*WID, 20*WID)
                                          textColor:UIColorFromRGB(0x2C2C2C)
                                               font:FW_XWID_FONT(14)
                                               text:@"信誉评级:"];
        _lblClassification.center = CGPointMake(self.imgIcon.center.x, CGRectGetMaxY(self.imgIcon.frame) + 20*WID);
        _lblClassification.textAlignment = NSTextAlignmentCenter;
    }
    return _lblClassification;
}




- (UILabel *)lblName{
    if (!_lblName) {
        _lblName = [self createLableFrame:CGRectMake(105*WID, 26*WID, 200, 20)
                                textColor:UIColorFromRGB(0xA4AAB3)
                                     font:FW_XWID_FONT(14)
                                     text:@"姓名:暂无数据"];
    }
    return _lblName;
}

- (UILabel *)lblProfession{
    if (!_lblProfession) {
        _lblProfession = [self createLableFrame:CGRectMake(105*WID, 26*WID + 20*WID, 200, 20)
                                      textColor:UIColorFromRGB(0xA4AAB3)
                                           font:FW_XWID_FONT(14)
                                           text:@"工种:暂无数据"];
    }
    return _lblProfession;
}

- (UILabel *)lblPrice{
    if (!_lblPrice) {
        NSString *price = [NSString stringWithFormat:@"价格:%ld元/每天", (long)_price];
        _lblPrice = [self createLableFrame:CGRectMake(105*WID, 26*WID + 2*20*WID, 120*WID, 20*WID)
                                 textColor:UIColorFromRGB(0xA4AAB3)
                                      font:FW_XWID_FONT(14)
                                      text:price];
    }
    return _lblPrice;
}




- (UILabel *)lblIDnumber{
    if (!_lblIDnumber) {
        _lblIDnumber = [self createLableFrame:CGRectMake(105*WID, 26*WID + 3*20*WID,250, 20)
                                    textColor:UIColorFromRGB(0xA4AAB3)
                                         font:FW_XWID_FONT(14)
                                         text:@"身份证号:330173666323238715"];
    }
    return _lblIDnumber;
}




- (UIButton *)btnSignIn{
    if (!_btnSignIn) {
        _btnSignIn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnSignIn.frame = CGRectMake(300*WID, 30*WID, 46*WID, 39*WID);
        [_btnSignIn setTitle:@"签到" forState:UIControlStateNormal];
        [_btnSignIn.layer setMasksToBounds:YES];
        [_btnSignIn.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
        [_btnSignIn setTitle:@"已签到" forState:UIControlStateDisabled];
        _btnSignIn.titleLabel.font = FW_XWID_FONT(14);
        [_btnSignIn setBackgroundImage:FW_IMAGE(@"绿色按钮") forState:UIControlStateNormal];
        [_btnSignIn setBackgroundImage:FW_IMAGE(@"yanzhengma") forState:UIControlStateDisabled];
        [_btnSignIn addTarget:self action:@selector(btnSignInAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnSignIn;
}


- (UILabel *)lblCheckInCount{
    if (!_lblCheckInCount) {
        _lblCheckInCount = [self createLableFrame:CGRectMake(282*WID, 70*WID, 80*WID, 20*WID)
                                 textColor:UIColorFromRGB(0xA4AAB3)
                                      font:FW_XWID_FONT(11)
                                      text:@"暂无数据"];
        _lblCheckInCount.textAlignment = NSTextAlignmentCenter;
    }
    return _lblCheckInCount;
}




-(void)setModel:(WorkerInfoModel *)model{
    _model = model;
    
    NSURL *url = [NSURL URLWithString:_model.avatar];
    [self.imgIcon sd_setImageWithURL:url placeholderImage:AVATARPLACEHOLDER];
    self.lblClassification.text = [NSString stringWithFormat:@"信用评级:%@", _model.rating_name];
    self.lblName.text = [NSString stringWithFormat:@"姓名:%@", _model.real_name];
    self.lblProfession.text = [NSString stringWithFormat:@"工种:%@", _model.type_name];
    self.lblPrice.text = [NSString stringWithFormat:@"价格:%@", _model.price];
    self.lblIDnumber.text = [NSString stringWithFormat:@"身份证号:%@", _model.idcard];
    self.price = _model.price.intValue;
    self.lblCheckInCount.text = [NSString stringWithFormat:@"已签到%d天", _model.check_in_count.intValue];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate = [dateFormatter dateFromString:_model.check_in_day];
    self.btnSignIn.enabled = ![destDate isDateToday];
  
}





- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
